<?php
namespace app\api\controller;
class Social extends Common{
	function upload(){
		$file=request()->file('file');
		if($file){
			p($this->uploads($file));
		}
		
	}
	
	/**
	 * 添加社交好友数据
	 * @param integer $soft_id  所属账号ID
	 * @param json   $data=[ 需要添加的数据(json)
     *        number     varchar(50)   社交号
	 *        media_id   varchar(200)   头像地址
	 *        name       varchar(50)   用户名(必选)
	 *        email      varchar(50)   邮箱
	 *        sex        tinyint(1)    性别：0女、1男、2保密
	 *        age        tinyint(3)    年龄
	 *        tel        int(11)       手机号码
	 *        head_img	 varchar(500)  头像地址
	 *        city   	 int(11)       城市
     *        area       int(11)       区/县
	 *        type    	 tinyint(1)    0：用户，1：群组 2:小程序
	 * ]  
	 */
    function addSocial($soft_id=0,$data=''){
    	$data=json_decode($data,true);
    	if($soft_id<=0||empty($data)){
    		return json(lang('Data does not exist'),1);
    	}

    	$time=time();
    	$updateData=[];
        $member_id=$this->user['id'];

    	if(is_integer(key($data))){ //批量添加
            $isGroup=isset($data[0]['type'])&&$data[0]['type'];
            if($isGroup){
                // 由于群组 没有唯一的号码识别（例：微信号）每次更新均做完全替换处理
                model($this->table)->del(['soft_id'=>$soft_id,'type'=>1]);
            }
    		foreach($data as $k => $v){
                $has=[];
                if(!$isGroup){
                    $map=['member_id'=>$member_id,'soft_id'=>$soft_id];
                    $map['number']=$v['number'];
        			// 验证该好友是否添加
        			$has=model($this->table)->read($map);
                }
    			if(!empty($has)){
                    $v['id']=$has['id'];
    				$updateData[]=$v;
    				unset($data[$k]);
    			}else{
                    $data[$k]['member_id']=$member_id;
    				$data[$k]['soft_id']=$soft_id;
			    	$data[$k]['city_id']=isset($v['city'])?$this->getCityId($v['city']):0;
			    	$data[$k]['area_id']=isset($v['area'])?$this->getCityId($v['area'],false):0;
			    	$data[$k]['s_time']=$time;
			    }
    		}
    	}else{
            $map=['member_id'=>$member_id,'soft_id'=>$soft_id];
            if(isset($data['type'])&&$data['type']){ //群
                //群并不存在唯一的识别码 不应该做此更新直接做批量替换
                $map['name']=$data['name'];
            }else{
                $map['number']=$data['number'];
            }
            $data['member_id']=$member_id;
    		$data['soft_id']=$soft_id;
	    	$data['city_id']=isset($data['city'])?$this->getCityId($data['city']):0;
	    	$data['area_id']=isset($data['area'])?$this->getCityId($data['area'],false):0;
	    	$data['s_time']=$time;
	    	// 验证该好友是否添加(做更新处理)
    		$has=model($this->table)->read($map);
			if(count($has)){
				$data['id']=$has['id'];
				$updateData=$data;
				$data=[];
			};
    	}

    	if(!empty($updateData)){
    		model($this->table)->edit($updateData);
    	}
    	//soft_id 所属账号ID
    	if(!empty($data)){
    		model($this->table)->add($data);
    	}
    	return json(lang('Successful operation'));
    }
    /**
     * 删除微信好友
     * @param  integer $soft_id 账号ID
     * @param  string  $numbers  微信号的JSON字符串 {0:'nums1',1:'nums2'}
     * @param  integer $type   是否群组
     * @return json
     */
    function delSocial($soft_id=0,$numbers='',$type=0){
    	$numbers=json_decode($numbers,true);
    	
    	if($soft_id<=0||empty($numbers)){
    		return json(lang('Data does not exist'),1);
    	}
        $field=$type>0?'name':'number';
    	$where=[
    		'soft_id'=>$soft_id,
    		$field=>[$field,'in',$numbers]
    	];
    	$del=model($this->table)->del($where);
    	return json(lang('Successful operation'));
    }

    
}
